package com.feng.show.service.impl;

import com.feng.show.comm.ServerResponse;
import com.feng.show.dao.AgentMapper;
import com.feng.show.entity.Agent;
import com.feng.show.entity.AgentExample;
import com.feng.show.service.IAgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service("IAgentService")
public class AgentServiceIpml implements IAgentService {
  @Autowired
  AgentMapper agentMapper;


  @Override
  public ServerResponse<List<Agent>> getAgentList(String userId) {

    List<Agent> agents = null;
    try {
      AgentExample agentExample=new AgentExample();
      agentExample.createCriteria().andFlagDelEqualTo("0");
      agents = agentMapper.selectByExample(agentExample);
    } catch (Exception e) {
      ServerResponse.createByErrorMsg("查询失败");
    }
    if (agents != null && agents.size() > 0) {
      return ServerResponse.createBySuccesMsg(agents);
    } else {
      return ServerResponse.createBySuccessMsg("无数据");
    }
  }

  @Override
  public ServerResponse<String> addMerchant(String userId, Agent agent) {

//    int status = agentMapper.insert(agent);
    int status =agentMapper.insertSelective(agent);
    if (status > 0) {
      return ServerResponse.createBySuccessMsg("添加成功");
    } else {
      return ServerResponse.createByErrorMsg("添加失败");
    }

  }

  @Override
  public ServerResponse<String> modifyMerchant(String userId, Agent agent) {
    int status =
      agentMapper.updateByPrimaryKeySelective(agent);
    if (status > 0) {
      return ServerResponse.createBySuccessMsg("修改成功");
    } else {
      return ServerResponse.createByErrorMsg("修改失败");
    }

  }

  @Override
  public ServerResponse<String> delMerchant(String userId, Agent agent) {
    //删除经销商
    if (agent == null || agent.getId() == null) {
      return ServerResponse.createByErrorMsg("参数错误");
    }
    agent.setFlagDel("2");
    agent.setModifyTime(new Date());
    int status = agentMapper.updateByPrimaryKeySelective(agent);
    if (status > 0) {
      return ServerResponse.createBySuccessMsg("删除成功");
    } else {
      return ServerResponse.createByErrorMsg("删除失败");
    }
  }
}
